package dice.sinanya.dice.manager;

import com.forte.qqrobot.beans.messages.types.MsgGetTypes;
import dice.sinanya.entity.EntitySendToMaster;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.exceptions.NotMasterException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import static dice.sinanya.system.MessagesSystem.NONE;
import static dice.sinanya.system.MessagesSystem.loginInfo;
import static dice.sinanya.system.MessagesTag.TAG_ANSWER;
import static dice.sinanya.system.MessagesTag.TAG_SEND;
import static dice.sinanya.tools.checkdata.CheckIsNumbers.isNumeric;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entityBanProperties;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entitySystemProperties;
import static dice.sinanya.tools.getinfo.GetTime.getNowString;
import static dice.sinanya.tools.makedata.MakeMessages.deleteTag;
import static dice.sinanya.tools.makedata.Sender.sender;
import static dice.sinanya.tools.makedata.StringFormatById.stringFormatById;

public class SendToMaster {
    private static final Logger log = LogManager.getLogger(SendToMaster.class.getName());
    private final EntityTypeMessages entityTypeMessages;

    public SendToMaster(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    public void send() throws NotMasterException {
        if (entityBanProperties.getMaster().size() == 0) {
            throw new NotMasterException(entityTypeMessages);
        }

        String tag = TAG_SEND;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        EntitySendToMaster entitySendToMaster = new EntitySendToMaster();
        entitySendToMaster.setBotId(String.valueOf(loginInfo.getLoginId()));
        entitySendToMaster.setDate(getNowString());
        entitySendToMaster.setQqId(entityTypeMessages.getFromQqString());
        entitySendToMaster.setMsgGetTypes(entityTypeMessages.getMsgGetTypes());
        if (entityTypeMessages.getMsgGetTypes() != MsgGetTypes.privateMsg) {
            entitySendToMaster.setGroupId(entityTypeMessages.getFromGroupString());
        }
        if (msg.equals(NONE)) {
            sender(entityTypeMessages, entitySystemProperties.getSendMustInsert(entityTypeMessages.getFromGroupString()));
            return;
        }
        entitySendToMaster.setMessages(msg);
        sender(entityTypeMessages, stringFormatById(entitySystemProperties.getSendAlready(entityTypeMessages.getFromGroupString()), msg));
        for (String master : entityBanProperties.getMaster()) {
            if (isNumeric(master)) {
                entityTypeMessages.getMsgSender().SENDER.sendPrivateMsg(master, entitySendToMaster.build());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

    public void answer() throws NotMasterException {
        if (!entityBanProperties.getMaster().contains(entityTypeMessages.getFromQqString())) {
            throw new NotMasterException(entityTypeMessages);
        }
        String tag = TAG_ANSWER;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        if (!msg.contains("|") || msg.split("\\|").length == 2) {
            String target = msg.split("\\|")[0];
            String messages = msg.split("\\|")[1];
            if (messages.equals(NONE) || !isNumeric(target)) {
                sender(entityTypeMessages, entitySystemProperties.getAnswerFormatError(entityTypeMessages.getFromGroupString()));
                return;
            }
            entityTypeMessages.getMsgSender().SENDER.sendPrivateMsg(target, "由骰主:\t" + entityTypeMessages.getFromQqString() + "反馈结果为:\t" + messages);
            sender(entityTypeMessages, "已成功发送");
        }

    }
}
